package com.niit.controller.common;

import com.niit.model.*;
import com.niit.service.TeacherNewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author 王铭
 * #Date 2021/6/23 - 18:27
 */
@Controller
@SessionAttributes(value = {"current", "teacher"})
public class TeacherNewController {


    TeacherNewService teacherNewService;

    @Autowired
    public void setTeacherNewService(TeacherNewService teacherNewService) {
        this.teacherNewService = teacherNewService;
    }

    @ModelAttribute("date")
    public Date date() {
        return new Date();
    }

    @GetMapping("main")
    public String main() {
        return "main";
    }

    @ModelAttribute("teacher")
    public void teacher(Model model) {
        Teacher teacher = new Teacher();
        teacher.setName("王铭");
        teacher.setAge(21);
        teacher.setSex("male");
        model.addAttribute(teacher);

    }

    @GetMapping("current")
    public String getCurrentPage(@ModelAttribute("current") String current) {
        if (current != null)
            return current;
        return "redirect:papers-uncheck";
    }

    @GetMapping("papers-uncheck")
    public String papersUncheck(Model model, @ModelAttribute Teacher teacher) {
        List<ExamPaper> examPapers = teacherNewService.findExamPapersUnChecked(teacher);//已结束但未批改
        model.addAttribute(examPapers);
        return "papers";
    }

    @GetMapping("check/{id}")
    public String check(@PathVariable("id") Integer id, Model model, @ModelAttribute Teacher teacher) {
        model.addAttribute(teacherNewService.checkPaper(id));//考卷id
        return papersUncheck(model, teacher);
    }

    @GetMapping("papers-checked")
    public String paperChecked(Model model, @ModelAttribute Teacher teacher) {
        List<ExamPaper> examPapers = teacherNewService.findExamPapersChecked(teacher);//已结束但已批改
        model.addAllAttributes(examPapers);
        model.addAttribute("checked");
        return "papers";
    }

    @GetMapping("view-mark/{id}")
    public String vm(@PathVariable("id") Integer id, Model model) {
        model.addAttribute(teacherNewService.viewMark(id));//试卷id
        return "view-mark";
    }

    @GetMapping("view-paper-detail/{paperId}")
    public String vmd(@PathVariable("paperId") Integer paperId, Model model) {
        model.addAttribute(teacherNewService.viewPaperDetail(paperId));
        return "view-paper-detail";
    }

    @GetMapping("exam-papers")
    public String examPapers(Model model, @ModelAttribute Teacher teacher) {
        model.addAttribute(teacherNewService.examPapers(teacher));
        return "exam-papers";
    }

    @GetMapping("exam-paper/{id}")
    public String examPaper(@PathVariable("id") Integer id, Model model) {
        model.addAttribute(teacherNewService.getExamPaper(id));//考卷id
        return "exam-paper";
    }

    @PostMapping("change-exam-paper")
    public String examPaperC(ExamPaper examPaper, Model model, String start_day, String start_time, String end_time, String end_day) {
        Date start_date = new Date();
        Date end_date = new Date();
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-ddhh:mm");
        try {
            start_date = dateFormat.parse(start_day + start_time);
            end_date = dateFormat.parse(end_day + end_time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        examPaper.setLimit_start_time(start_date);
        examPaper.setLimit_end_time(end_date);
        model.addAttribute(teacherNewService.changeExamPaper(examPaper));
        return "exam-paper";
    }

    @GetMapping("new-exam-paper")
    public String getExamPaperA(Model model) {
//        model.addAttribute(teacherService.addExamPaper(examPaper));//返回考卷对象
        model.addAttribute(new ExamPaper());
        return "exam-paper";
    }

    @PostMapping("new-exam-paper")
    public String examPaperA(ExamPaper examPaper, Model model, String start_day, String start_time, String end_time, String end_day) {
        Date start_date = new Date();
        Date end_date = new Date();
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-ddhh:mm");
        try {
            start_date = dateFormat.parse(start_day + start_time);
            end_date = dateFormat.parse(end_day + end_time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        examPaper.setLimit_start_time(start_date);
        examPaper.setLimit_end_time(end_date);
        model.addAttribute(teacherNewService.addExamPaper(examPaper));//返回考卷对象
        return "exam-paper";
    }


    @GetMapping("questions-for-add/{paperId}")
    public String questionForAdd(@PathVariable("paperId") Integer paperId, Model model, @ModelAttribute Teacher teacher) {
        model.addAttribute(teacherNewService.queryQuestionsForAdd(paperId, teacher));//输入考卷id返回未添加的考题列表
        model.addAttribute(paperId);//标记添加考题界面
        return "questions";
    }

    @GetMapping("questions-for-add/{paperId}/{questionId}")
    public String questionAdd(@PathVariable("paperId") Integer paperId, @PathVariable("questionId") Integer id, Model model, @ModelAttribute Teacher teacher) {
        model.addAttribute(teacherNewService.addQuestionForExam(paperId, id, teacher));//添加paper question,更新question count;返回考卷对象
        model.addAttribute(paperId);//标记添加考题界面
        return "exam-paper"; //返回考卷编辑界面
    }

    @GetMapping("questions")
    public String questions(Model model, @ModelAttribute Teacher teacher) {
        model.addAttribute(teacherNewService.queryQuestions(teacher));//考题列表
        return "questions";
    }

    @GetMapping("view-a-question/{id}")
    public String viewAQuestion(@PathVariable("id") Integer id, Model model) {
        model.addAttribute(teacherNewService.getQuestion(id));
        return "view-a-question";
    }


    @PostMapping("change-a-question")
    public String changeAQuestion(Question question, Model model, String selection, String content) {
        String se[] = selection.split(",");
        String ct[] = content.split(",");
        for (int i = 0; i < se.length; i++) {
            Selection selection1 = new Selection();
            selection1.setThe_selection(se[i]);
            selection1.setThe_content(ct[i]);
            teacherNewService.appeendSelection(question, selection1);
        }
        model.addAttribute(teacherNewService.changeQuestion(question));
        return "view-a-question";
    }

    @GetMapping("add-a-question")
    public String addAQuestion(Model model) {
        model.addAttribute(new Question());
        return "view-a-question";
    }

    @PostMapping("add-a-question")
    public String addAQuestion(Question question, Model model) {
        model.addAttribute(teacherNewService.addQuestion(question));
        return "view-a-question";
    }

    @GetMapping("class-list")
    public String Class(@ModelAttribute Teacher teacher, Model model) {
        model.addAttribute(teacherNewService.getClasses(teacher));
        return "class-list";
    }


    @GetMapping("class-list-pageful")
    public String classPageful(@ModelAttribute Teacher teacher, Model model) {
        return classPageful(teacher, model, 15, 1);
    }

    @GetMapping("class-list-pageful/{aPageNum}")
    public String classPageful(@ModelAttribute Teacher teacher, Model model, @PathVariable("aPageNum") Integer aPageNum, @RequestParam("index") Integer index) {
        model.addAttribute(teacherNewService.getClassesPageful(teacher, aPageNum, index));
        return "class-list";
    }


    @GetMapping("change-class/{id}")
    public String editClass(Model model, @PathVariable("id") Integer id) {
        model.addAttribute(teacherNewService.editClass(id));
        return "change-class";
    }

    @GetMapping("delete-class/{id}")
    public String deleteClass(Model model, @PathVariable("id") Integer id, @ModelAttribute Teacher teacher) {
        model.addAttribute(teacherNewService.deleteClass(id));
        return classPageful(teacher, model);
    }

    @PostMapping("change-class")
    public String changeClass(Classes classes, Model model) {
        model.addAttribute(teacherNewService.changeClass(classes));
        return "change-class";
    }

    @PostMapping("add-class")
    public String addClass(Classes classes, Model model) {
        model.addAttribute(teacherNewService.addClass(classes));
        return "change-class";
    }

    @GetMapping("add-class")
    public String getAddClass(Model model) {
        model.addAttribute(new Classes());
        return "change-class";
    }

    @GetMapping("class-student")
    public String classStudent() {
        return "class-student";
    }

    @PostMapping("class-student")
    public String classStudent(@RequestParam("code") Integer code, Model model) {
        model.addAttribute(teacherNewService.getStudents(code));
        model.addAttribute(teacherNewService.getClasse(code));
        return "class-student";
    }

    @RequestMapping("class-student-pageful")
    public String classStudentPageful(@RequestParam("code") Integer code, Model model) {
        return classStudent(code, model, 15, 1);
    }


    @GetMapping("class-student-pageful/{aPageNum}/{code}")
    public String classStudent(@PathVariable("code") Integer code, Model model, @PathVariable("aPageNum") Integer aPageNum, @RequestParam("index") Integer index) {
        model.addAttribute(teacherNewService.getStudentsPageful(aPageNum, index));
        model.addAttribute(teacherNewService.getClasse(code));
        return "class-student";
    }


    @GetMapping("class-student/{code}/{student}")
    public String classStudent(@PathVariable("code") Integer code, @PathVariable("student") Integer id, Model model) {
        model.addAttribute(teacherNewService.removeStudentFromClass(code, id));
        return classStudent(code, model);
    }

    @GetMapping("account-info")
    public String accountInfo() {
        return "account-info";
    }

    @PostMapping("account-info")
    public String ChangeAccountInfo(@ModelAttribute Teacher teacher, Model model) {
        model.addAttribute(teacherNewService.changeAccount(teacher));
        return "account-info";
    }

    @GetMapping("change-pwd")
    public String changePassword() {
        return "change-password";
    }

    @PostMapping("change-pwd")
    public String changePassword(Model model, String old,@RequestParam("new") String neW, String cfm) {
        model.addAttribute(teacherNewService.changePwd(old, neW, cfm));
        return "change-password";
    }
}
